<?php
/**
 * CLASS PAGING NAVIGATOR
 * @version 1.3
 * @author ThanhNV 
 * 
 * 
 * @desc 13May08
 * convert to php5 class
 * @desc 14Feb08:
 * change type of url:
 * + new format: ?mod=abc&act=def
 * + $this->sort_link
 * + $this->currPageNav
 * @desc 21Nov07:
 * change next, prev public function
 * @desc 08Sep07
 * first version
 * 
*/


class paging{
	private $sql;
	private $total_record;
	
	private $currPage;
	private $strPage;
	private $page_size;
	private $pagegroup_size;
	private $stylesheet;
	private $pageNav;
	private $sort_link;
	private $params;
	private $offset;
	private $jsFunc;
	
	
	public function __construct(){
		
	}
	
	public function init($sql, $total_record, $page_size, $pagegroup_size, $page="page", $stylesheet = "paging"){
		$currPage = $_GET[$page];
		
		$this->sPage = $page;
		$this->currPage = (intval($currPage) > 0) ? intval($currPage) : 1;
		
		$this->currPage_size = intval($page_size) == 0 ? PAGING_SIZE : intval($page_size);
		$this->currPagegroup_size = intval($pagegroup_size) == 0 ? PAGING_GROUP_SIZE : intval($pagegroup_size);
		
		$this->stylesheet = $stylesheet;
		$this->jsFunc = $jsFunc;
		
		$this->total_record = $total_record;
		//$this->getTotalRecord($sql);
		
		$ArrRemove = array('');
		$this->params = $this->getQueryString();
		
		
		$ArrRemove2 = array('SortCol','SortType');
		/*$this->sort_link = $_SERVER['PHP_SELF'] . "?" . $this->getQueryString($ArrRemove2);*/
		$this->sort_link = "?" . $this->getQueryString($ArrRemove2);
		
		$ArrRemove3 = array($this->sPage);
		/*$this->currPageNav = $_SERVER['PHP_SELF'] . "?" . $this->getQueryString($ArrRemove3);*/
		$this->currPageNav = "?" . $this->getQueryString($ArrRemove3);
		
		$this->offset = ($this->currPage - 1) * $this->currPage_size;
		$this->sql = $sql." LIMIT ".$this->offset.", ".$this->currPage_size;		
	} 
	
	public function getTotalRecord (){
		return $this->total_record;
	}
	public function getSql(){
		return $this->sql;
	}
	
	public function getQueryString ($ArrRemove = array()){
		global $_GET;
		global $_SERVER;
		
		$QueryString = "" ;
		
		if(count($_GET)>0){
			foreach($_GET as $id => $value){
				if(!in_array($id,$ArrRemove) && $value != '') {
					if(is_array($value)){
						if(count($value)>0){
							foreach ($value as $itemValue){
								$QueryString .= "&{$id}[]={$itemValue}" ;
							}
						}
					}else{
						$QueryString .= "&{$id}={$value}" ;
					}
					
				}
			}
		}
		return $QueryString;
	}
	
	
	public function setSortField ($FieldSort, $ShowName){
		global $_GET;
		global $_SERVER;
		
		$Image = "";
		$Type = "DESC";
		if($FieldSort == $_GET[SortCol]){
			if($_GET[SortType] == "DESC"){
				$Image = "<img src='".PAGING_IMAGE_DESC."' border='0' style='padding-right:5px;' >";
				$Type = "ASC";
			}
			else{
				$Image = "<img src='".PAGING_IMAGE_ASC."' border='0' style='padding-right:5px;' >";
				$Type = "DESC";
			}
		}
		$SortLink = $this->sort_link . "&SortCol=" . $FieldSort . "&SortType=" . $Type ;
		
		return "<a href='".$SortLink."' class='sort'>".$Image.$ShowName."</a>";
	}
	
	public function navigator() 
	{
		$td_orther = '<td class="others" style="cursor:pointer" ';
		
		$andpage=(strrpos ($this->currPageNav,"?")===false)?"?{$this->sPage}=":"&{$this->sPage}=";
		$page_total = floor(($this->total_record-1)/$this->currPage_size)+1;
		if(!$this->currPage || $this->currPage > $page_total || $this->currPage < 1)
			$this->currPage = 1;
		$group = floor(($this->currPage-1)/$this->currPagegroup_size)+1;
		$start_page = (($group-1)*$this->currPagegroup_size)+1;
		$end_page = $start_page+$this->currPagegroup_size-1;
		if ($end_page>$page_total) 
			$end_page = $page_total;
		if ($page_total>1) {
			$from = $this->offset + 1;
			$to = ($this->total_record < $this->offset + $this->currPage_size) ? $this->total_record :  $this->offset + $this->currPage_size;
			
			$str='<table class="'.$this->stylesheet.'"><tr>';
			$str.="<td class=\"info\">{$from} - {$to} (of {$this->total_record}) </td>";
			if ($end_page>$this->currPagegroup_size)
				$start_group = $this->currPagegroup_size;
			else
				$start_group = 0;
			if ($this->currPage>1){
				$str.= $td_orther.' onclick="javascript: location.href=\''.$this->currPageNav.$andpage.($this->currPage-1).'\'">Prev</td>';
			}	
			for ( $i=$start_page ; $i <= $end_page ;$i++){
				$j=$i+$this->currPage_size;
				$begin = ($i-1)*$this->currPage_size + 1;
				$end = $begin + $this->currPage_size - 1; 
				if ($i == $this->currPage)
					$str.= ' <td class="selected">'.$i.'</td> ';
				else{
					$str.= $td_orther.' onclick="javascript: location.href=\''.$this->currPageNav.$andpage.$i.'\'">'.$i.'</td> ';		
				}
			}
			if ($page_total-$end_page>$this->currPagegroup_size)
				$end_group = $this->currPagegroup_size;
			else
				$end_group = $page_total-$end_page;
			if ($this->currPage<$page_total){
				$str.= $td_orther.' onclick="javascript: location.href=\''.$this->currPageNav.$andpage.($this->currPage+1).'\'">Next</td>';
			}
			$str.='</tr></table>';
			return $str;
		}	
	}
	
	function navigatorAjax($ajaxRequestFunction) 
	{
		$td_orther = '<td class="others" onmouseover="javascript: this.className=\'selected\';" onmouseout="javascript: this.className=\'others\';" ';
		
		$andpage=(strrpos ($this->currPageNav,"?")===false)?"?{$this->sPage}=":"&{$this->sPage}=";
		$page_total = floor(($this->total_record-1)/$this->currPage_size)+1;
		if(!$this->currPage || $this->currPage > $page_total || $this->currPage < 1)
			$this->currPage = 1;
		$group = floor(($this->currPage-1)/$this->currPagegroup_size)+1;
		$start_page = (($group-1)*$this->currPagegroup_size)+1;
		$end_page = $start_page+$this->currPagegroup_size-1;
		if ($end_page>$page_total) 
			$end_page = $page_total;
		if ($page_total>1) {
			$from = $this->offset + 1;
			$to = ($this->total_record < $this->offset + $this->currPage_size) ? $this->total_record :  $this->offset + $this->currPage_size;
			
			$str='<table class="'.$this->stylesheet.'"><tr>';
			$str.="<td class=\"info\">{$from} - {$to} (of {$this->total_record}) </td>";
			if ($end_page>$this->currPagegroup_size)
				$start_group = $this->currPagegroup_size;
			else
				$start_group = 0;
			if ($this->currPage>1){
				$addPage = str_replace('{url}',$this->currPageNav.$andpage.($this->currPage-1),$ajaxRequestFunction);
				$str.= $td_orther.' onclick="javascript: '.$addPage.'">Prev</td>';
			}	
			for ( $i=$start_page ; $i <= $end_page ;$i++){
				$j=$i+$this->currPage_size;
				$begin = ($i-1)*$this->currPage_size + 1;
				$end = $begin + $this->currPage_size - 1; 
				if ($i == $this->currPage)
					$str.= ' <td class="selected">'.$i.'</td> ';
				else{
					$addPage = str_replace('{url}',$this->currPageNav.$andpage.$i,$ajaxRequestFunction);
					$str.= $td_orther.' onclick="javascript: '.$addPage.'">'.$i.'</td> ';		
				}
			}
			if ($page_total-$end_page>$this->currPagegroup_size)
				$end_group = $this->currPagegroup_size;
			else
				$end_group = $page_total-$end_page;
			if ($this->currPage<$page_total){
				$addPage = str_replace('{url}',$this->currPageNav.$andpage.($this->currPage+1),$ajaxRequestFunction);
				$str.= $td_orther.' onclick="javascript: '.$addPage.'">Next</td>';
			}
			$str.='</tr></table>';
			return $str;
		
		}	
	}
	
	
}
?>